//
//  ArgumentStore.m
//  CustomerServer
//
//  Created by 涂俊 on 2019/4/12.
//  Copyright © 2019年 Facebook. All rights reserved.
//

#import "ArgumentStore.h"
#import "SuperSDKPluginTools/SSDKPluginJsonUtil.h"

@interface ArgumentStore ()
@property(nonatomic, strong)NSString *type;
@end
@implementation ArgumentStore

{
  NSMutableDictionary *cacheMap;
}

static ArgumentStore *instance = nil;
static ArgumentStore *opInstance = nil;
NSDictionary *_pvConfig = nil;
ArgumentStore *_current = nil;

+ (instancetype) getInstance {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    instance = [[self alloc] init];
      instance.type = @"0";
  });
  return instance;
}

+ (instancetype)opInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        opInstance = [[self alloc] init];
        opInstance.type = @"1";
    });
    return opInstance;
}

+ (instancetype)instanceForKey:(NSString *)key {
    NSString *gameAppId = [[self config] objectForKey:@"appId"];
    if ([key isEqual:gameAppId]) {
        return [ArgumentStore getInstance];
    }
    NSString *apAppId = [[self config] objectForKey:@"opAppId"];
    if ([key isEqual:apAppId]) {
        return [ArgumentStore opInstance];
    }
    return nil;
}

+ (NSDictionary *)config {
    if (_pvConfig) {
        return _pvConfig;
    } else {
        NSDictionary *config = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"SSDKConfig"];
        if (config == nil) {
            return nil;
        }
        return [config objectForKey:@"PLUGIN_Config_VipCustomer"];
    }
}

+ (void)setCurrent:(nullable ArgumentStore *)current {
    if (_current != current) {
        if ([_current.imLoginResult isEqual:@"ok"]) {
            _current.imLoginResult = @"fail";
        }
        _current = current;
    }
}

+ (nullable ArgumentStore *)current {
    return _current;
}

- (id)init {
  if (self = [super init]) {
    cacheMap = [[NSMutableDictionary alloc] init];
  }
  return self;
}

- (NSString *) getCache:(NSString *)key {
  return [cacheMap objectForKey:key];
}

- (void) setCache:(NSString *)value forKey:(NSString *)key {
  [cacheMap setObject:value forKey:key];
}

// 将环信用户名和密码，持久化到本地
- (void)saveUserWithUid:(NSString *)uid
                 roleId:(NSString *)roleId
             imUsername:(NSString *)imUsername
             imPassword:(NSString *)imPassword {
    if (imUsername == nil || imUsername.length == 0 || imPassword == nil || imPassword.length == 0) {
        return;
    }
    NSString *key = [NSString stringWithFormat:@"vip_%@_%@", uid, roleId];
    NSString *value = [SSDKPluginJsonUtil toJsonWithObject:@[imUsername,imPassword]];
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:_appId];
    [defaults setObject:value forKey:key];
}

// 获取环信账号名和密码
- (NSArray *)getUserByUid:(NSString *)uid
                   roleId:(NSString *)roleId {
    NSString *key = [NSString stringWithFormat:@"vip_%@_%@", uid, roleId];
    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:_appId];
    NSString *value = [defaults objectForKey:key];
    if (value == nil || value.length == 0) {
        return nil;
    }
    NSArray *result = [SSDKPluginJsonUtil toObjectWithJson:value];
    return result;
}

@end
